package com.gary.shiro;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;

/**
 * @author peng_rui
 * @date 2022/6/14
 */
@Slf4j
public class MysqlConnTest {

    public Connection getConnection(String driver, String url, String name, String password) throws InterruptedException {

        for (int i = 1; i <= 3 ; i++) {
            try {
                Class.forName(driver);
                Connection connection = DriverManager.getConnection(url,name,password);
                return connection;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                log.error("第{}次:连接数据库发生异常",i);
            } catch (SQLException e) {
                e.printStackTrace();
                log.error("第{}次:连接数据库发生异常",i);
            }
            Thread.sleep(100);
        }

        return null;

    }

    public Boolean mysqlAdd(Connection connection){
        for(int i=1;i<=3;i++){
            try {
                Statement statement = connection.createStatement();
                Integer row = 1;
                // 新增探测
                String sql = "insert into t_ac_mysql_healthy_test(`name`) values ('testMysql')";
                int updateResult = statement.executeUpdate(sql);
                if (row.equals(updateResult)) {
                    log.info("数据库插入数据正常");
                    return Boolean.TRUE;
                }
                Thread.sleep(100);
            } catch (Exception e) {
                log.error("第{}次:数据库插入异常",i, e);
            }
        }
        return Boolean.FALSE;

    }

    public Boolean mysqlFind(Connection connection) {
        // 添加异常重试机制
        for (int i = 1; i <= 3; i++) {
            try {
                Statement statement = connection.createStatement();
                // 查询探测
                String sql = "select * from t_ac_mysql_healthy_test where `name` = 'testMysql'";
                ResultSet resultSet = statement.executeQuery(sql);
                if (!resultSet.isClosed()) {
                    log.info("数据库查询正常");
                    return true;
                }
                Thread.sleep(100);
            } catch (Exception e) {
                log.error("第{}次:数据库查询异常", i, e);
            }
        }
        return false;

    }

    public Boolean mysqlDelete(Connection connection) {

        for (int i = 1; i <= 3 ; i++) {
            try {
                Statement statement = connection.createStatement();
                // 新增探测
                String sql = "delete from t_ac_mysql_healthy_test where `name` = 'testMysql'";
                int row = statement.executeUpdate(sql);
                if (row >= 0) {
                    return Boolean.TRUE;
                }
                Thread.sleep(100);
            } catch (Exception e) {
                log.error("数据库删除异常", e);
            }
        }
        return Boolean.FALSE;

    }
    public static void main(String[] args) throws InterruptedException {
        MysqlConnTest service = new MysqlConnTest();
        Connection connection = service.getConnection("org.gjt.mm.mysql.Driver", "jdbc:mysql://120.55.99.182:3306/cloud2020", "root", "pengrui");
        Boolean aBoolean = service.mysqlAdd(connection);
        Boolean aBoolean1 = service.mysqlFind(connection);
        Boolean aBoolean2 = service.mysqlDelete(connection);
        System.out.println(aBoolean);
        System.out.println();
        System.out.println(aBoolean1);
        System.out.println();
        System.out.println(aBoolean2);
    }
}
